#!/bin/bash

# load install functions
. vserver.functions

WORKER_SCRIPTS_PATH=./worker-scripts
VSERVER_DIST=linux-image-vserver-686

# Check if sudo is installed on this machine.
function detect_sudo() {

	if ! isSudoAvailable ; then
		echo 
		echo "You have to install the sudo package to be able to install vserver."
		echo "Please install it or contact the system administrator."
		echo
		exit 1
	fi
}

function detect_management_tool() {

	if isAptAvailable
	then
		echo " apt tool detected. Preparing to install the packages: linux-image-vserver-686 util-vserver ssh "
		install_packages
		if isVServerAvailable 
		then
			echo 
			echo "================================="
			echo " Well done boy. VServer was succesfully installed"
			echo "================================="
			echo
		else 
			echo 
			echo "================================="
			echo " An error ocurred in the apt-get install process, vserver could not be installed. Calling Garbage collector ..."
			echo "================================="
			echo 
			remove_packages
		fi
	else
		echo 
		echo "================================="
		echo " A management tool (apt, yum etc) was not detected, the installation was aborted. "
		echo "================================="
		echo
	fi
}

function detect_kernel_install() {
 
	if isVServer_KernelAvailable 
	then
		echo
 		echo "================================="
		echo " Well done boy. VServer kernel was sucessfully installed, please reboot the machine."
		echo "================================="
		echo 
	else 
		echo
		echo "================================="
		echo " VServer kernel could not be installed please contact your revendor."
		echo "================================="
		echo
		exit 1
	fi 
}


function install_packages() {
	apt-get -y install $VSERVER_DIST
	apt-get -y --force-yes install --reinstall --purge util-vserver
}

function remove_packages() {
	apt-get -y remove $VSERVER_DIST util-vserver
}

function check_user() {

	su $WORKER_USER -c true
	if  [ $? -ne 0 ] ; then
		echo
		echo "User $WORKER_USER does not exist, you have to create it before installation or select another user." 
		exit 1
	fi
}


function installing_scripts() {
	# Moving Worker Scripts.
	cp $WORKER_SCRIPTS_PATH/* /usr/sbin/

	# Setting Execution Permission.
	chmod 755 /usr/sbin/vserver_prepare_allocation
	chmod 755 /usr/sbin/vserver_copy_files
	chmod 755 /usr/sbin/vserver_replace_vm_image
	chmod 755 /usr/sbin/vserver_initvm
	chmod 755 /usr/sbin/vserver_stopvm
	chmod 755 /usr/sbin/vserver_stop_preparing_allocation
	chmod 755 /usr/sbin/vserver_kill_tar_proc
	chmod 755 /usr/sbin/vserver_status
	chmod 755 /usr/sbin/vserver_exec
	chmod 755 /usr/sbin/vserver-info
	chmod 755 /usr/sbin/vnamespace
	chmod 755 /usr/sbin/vserver
	chmod 755 /usr/sbin/VSERVER_OPTS
	chmod 755 /usr/sbin/shellApp
}

# Adding permissions to sudoers file
function config_sudoers() {

	if [ -z "$(grep "$WORKER_USER ALL= NOPASSWD: /usr/sbin/vserver" /etc/sudoers)" ]; then 
		echo "$WORKER_USER ALL= NOPASSWD: /usr/sbin/vserver" >> /etc/sudoers
	fi

	if [ -z "$(grep "$WORKER_USER ALL= NOPASSWD: /usr/sbin/vserver_copy_files" /etc/sudoers)" ]; then 
		echo "$WORKER_USER ALL= NOPASSWD: /usr/sbin/vserver_copy_files" >> /etc/sudoers
	fi

	if [ -z "$(grep "$WORKER_USER ALL= NOPASSWD: /usr/sbin/vserver_replace_vm_image" /etc/sudoers)" ]; then 
		echo "$WORKER_USER ALL= NOPASSWD: /usr/sbin/vserver_replace_vm_image" >> /etc/sudoers
	fi

	if [ -z "$(grep "$WORKER_USER ALL= NOPASSWD: /usr/sbin/vserver_kill_tar_proc" /etc/sudoers)" ]; then 
		echo "$WORKER_USER ALL= NOPASSWD: /usr/sbin/vserver_kill_tar_proc" >> /etc/sudoers
	fi

	if [ -z "$(grep "$WORKER_USER ALL= NOPASSWD: /usr/sbin/vserver-info" /etc/sudoers)" ]; then 
		echo "$WORKER_USER ALL= NOPASSWD: /usr/sbin/vserver-info" >> /etc/sudoers
	fi

	if [ -z "$(grep "$WORKER_USER ALL= NOPASSWD: /usr/sbin/vnamespace" /etc/sudoers)" ]; then 
		echo "$WORKER_USER ALL= NOPASSWD: /usr/sbin/vnamespace" >> /etc/sudoers
	fi

}

function usage() {

	echo "Usage: install <workerUserName>"
	echo "For OurGrid updates and additional information, see the "
	echo "OurGrid Project home page at http://www.ourgrid.org/"

}

# main() - Checking for dependencies.

# Assign the variables
nargs=$#

if [ $nargs -eq 1 ]; then
	WORKER_USER=$1

else 
	usage
	exit 1
fi


echo
echo "================================="
echo "   Ready to begin installation   "
echo "================================="
echo
echo "==========================="
echo "   Checking dependencies   "
echo "==========================="
echo

echo -n "Checking for Sudo... "
detect_sudo
echo "OK"
echo

# main() - Configuring VServer.
echo
echo "================================================="
echo "   Configuring VServer"
echo "   Detecting distro package management tools ... "
echo "================================================="
echo

detect_management_tool
detect_kernel_install

echo
echo "====================================="
echo "   You're done. VServer is installed."
echo "====================================="
echo
# main() - Installing Worker Scripts.
echo
echo "====================================="
echo "   Installing Worker Scripts."
echo "====================================="
echo
check_user
installing_scripts
config_sudoers

echo
echo "====================================="
echo "   Environment is configured."
echo "====================================="
echo
